<?php

namespace app\admin\model;

use app\admin\model\halt\Cate;
use think\Config;
use think\Model;

class Wxpro extends Model
{
    // 表名
    protected $name = 'wxpro';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [
        'type_text',
        'fenlei_text',
        'kemu_text',
        'banben_text',
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['type'];
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getTypeList()
    {
        $data = Config::get('wxproduct');
        if(!isset($data['type']))
        {
            return [];
        }
        return array_column(json_decode($data['type'],true),'name','id');
    }

    public function getFenleiList()
    {
        return array_column(collection(Cate::field('id,name')->select())->toArray(), 'name','id');
    }

    public function getFenleiTextAttr($value, $data)
    {
        $value = $value ? $value : $data['fenlei'];
        $list = $this->getFenleiList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getKemuTextAttr($value, $data)
    {
        $value = $value ? $value : $data['kemu'];
        $list = $this->getFenleiList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getBanbenTextAttr($value, $data)
    {
        $value = $value ? $value : $data['banben'];
        $list = $this->getFenleiList();
        return isset($list[$value]) ? $list[$value] : '';
    }









}
